package com.zyy.smartcity1129.parking;

import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.zyy.smartcity1129.BaseFragment;
import com.zyy.smartcity1129.R;
import com.zyy.smartcity1129.data.ParkInfo;
import com.zyy.smartcity1129.util.Http;
import com.zyy.smartcity1129.util.RvAd;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName: ParkingFragment$
 * @Description: 作用
 * @Author: letquit
 * @CreateDate: 4/12/2021$
 * @Version: 1.0
 */
public class ParkingFragment extends BaseFragment {
    public static ParkInfo.RowsBean park;
    private RecyclerView rvPark;
    private Button btnPark;
    RvAd<ParkInfo.RowsBean> rvAd;

    @Override
    protected int layId() {
        return R.layout.f_park;
    }

    @Override
    protected String title() {
        right.setVisibility(View.VISIBLE);
        right.setText("。。。");
        return "停哪儿";
    }

    @Override
    protected boolean back() {
        return true;
    }

    @Override
    protected Boolean bottom() {
        return null;
    }

    @Override
    protected void init(View iv) {
        initView(iv);
        rvPark.setLayoutManager(new LinearLayoutManager(getContext()));
        th(() -> {
            List<ParkInfo.RowsBean> list = Http.getInstance().get("/prod-api/api/park/lot/list", null, ParkInfo.class, 1).getRows();
            List<ParkInfo.RowsBean> parkList = new ArrayList<>();
            List<ParkInfo.RowsBean> parkAllList = new ArrayList<>();
            parkList.addAll(list.stream().collect(Collectors.toList()).subList(0, 5));
            parkAllList.addAll(list.stream().sorted(Comparator.comparing(ParkInfo.RowsBean::getDistance)).collect(Collectors.toList()));
            ui(() -> {
                initAdapter(parkList);
                rvPark.setAdapter(rvAd);
                btnPark.setOnClickListener(view -> {
                    btnPark.setVisibility(View.GONE);
                    initAdapter(parkAllList);
                    rvPark.setAdapter(rvAd);
                });
            });
        });
        right.setOnClickListener(view -> {
            add(new ParkingRecordFragment());
        });
    }

    private void initAdapter(List<ParkInfo.RowsBean> parkList) {
        rvAd = new RvAd<ParkInfo.RowsBean>(parkList, R.layout.rv_base) {
            private void initView(View vi) {
                ig = (ImageView) vi.findViewById(R.id.ig);
                lineRvPatient = (LinearLayout) vi.findViewById(R.id.lineRvPatient);
                tv1 = (TextView) vi.findViewById(R.id.tv1);
                tv2 = (TextView) vi.findViewById(R.id.tv2);
                tv3 = (TextView) vi.findViewById(R.id.tv3);
                tv4 = (TextView) vi.findViewById(R.id.tv4);
                tv5 = (TextView) vi.findViewById(R.id.tv5);
                tv6 = (TextView) vi.findViewById(R.id.tv6);
                tv7 = (TextView) vi.findViewById(R.id.tv7);
                imgRvPatient = (ImageView) vi.findViewById(R.id.imgRvPatient);
                ig.setVisibility(View.GONE);
            }

            private ImageView imgRvPatient;
            private TextView tv7;
            private TextView tv6;
            private TextView tv5;
            private TextView tv4;
            private TextView tv3;
            private TextView tv2;
            private TextView tv1;
            private LinearLayout lineRvPatient;
            private ImageView ig;

            @Override
            protected void initR(View vi, ParkInfo.RowsBean rowsBean, int p) {
                initView(vi);
                tv1.setText(rowsBean.getParkName());
                tv2.setText("空位数量: " + rowsBean.getVacancy());
                tv3.setText("地址: " + rowsBean.getAddress());
                tv4.setText("收费费率: " + rowsBean.getRates() + "元/小时");
                tv5.setText("距离：" + rowsBean.getDistance() + "km");
                vi.setOnClickListener(v1->{
                    park=rowsBean;
                    add(new Parking1Fragment());
                });
            }
        };
    }

    private void initView(View iv) {
        rvPark = (RecyclerView) iv.findViewById(R.id.rvPark);
        btnPark = (Button) iv.findViewById(R.id.btnPark);
    }
}
